﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Xajhzwb.Components.Data;
using Xajhzwb.Components.Util;

namespace Xajhzwb
{
    public partial class FormWebPost : Form
    {
        public FormWebPost()
        {
            InitializeComponent();
        }

        private void FormPublish_Load(object sender, EventArgs e)
        {
            try
            {
                // load treeview
                loadTreeView();
            }
            catch(Exception ex)
            {
                DialogResult result = Utils.ShowMessageBox(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// load treeview
        /// </summary>
        private void loadTreeView()
        {
            List<ClassData> classList = DataHelper.GetClassDataList(Const.ClassType.Post.ToString());
            if(classList != null && classList.Count > 0)
            {
                foreach (ClassData classData in classList)
                {
                    TreeNode parentNode = new TreeNode();
                    parentNode.Name = "C" + classData.ClassID.ToString();
                    parentNode.Text = classData.ClassName;

                    List<WebPostData> postDataList = DataHelper.GetWebPostNameList(classData.ClassID);
                    if(postDataList != null && postDataList.Count > 0)
                    {
                        foreach(WebPostData postData in postDataList)
                        {
                            TreeNode childNode = new TreeNode();
                            childNode.Name = postData.ID.ToString();
                            childNode.Text = postData.Name.ToString();

                            parentNode.Nodes.Add(childNode);
                        }
                    }

                    treeViewWebPost.Nodes.Add(parentNode);
                }
            }

            //// default selected
            //COnfig config = new COnfig();
            //if(config.RecMg != "")
            //{
            //    treeViewWebPost.SelectedNode.Name = config.RecMg;
            //}
        }

        private void loadPostData(int ID)
        {
            WebPostData webPostData = DataHelper.GeWebtPostData(ID);
            if(webPostData != null)
            {
                label32.Text = webPostData.Name;
                txtUrlChk.Text = webPostData.UrlChk;
                txtUrlPost.Text = webPostData.UrlPost;
                chkIsReply.Checked = webPostData.IsReply;
                txtUrlReply.Text = webPostData.UrlReply;

                txtTitleMin.Text = webPostData.TitleMin.ToString();
                txtTitleMax.Text = webPostData.TitleMax.ToString();
                txtContentMin.Text = webPostData.ContentMin.ToString();
                txtContentMax.Text = webPostData.ContentMax.ToString();
                chkIsUbb.Checked = webPostData.IsUbb;
                chkIsCrlf.Checked = webPostData.IsCrlf;
                cboCodePage.SelectedValue = webPostData.CodePage;
                chkIsForcePost.Checked = webPostData.IsForcePost;
                rboPostSort1.Checked = (webPostData.PostSort == 1);
                rboPostSort2.Checked = (webPostData.PostSort == 2);
                chkIsSelfChk.Checked = webPostData.IsSelfChk;
                rboChkMode1.Checked = (webPostData.ChkMode == 0);
                rboChkMode2.Checked = (webPostData.ChkMode == 1);
                rboChkMode3.Checked = (webPostData.ChkMode == 2);
                chkIsYmUploadDir.Checked = webPostData.IsYmUploadDir;
                txtUploadDir.Text = webPostData.UploadDir;

                txtPostTitle.Text = webPostData.PostTitle;
                txtPostContent.Text = webPostData.PostContent;

                if(webPostData.PostParaList != null && webPostData.PostParaList.Count > 0)
                {
                    foreach(string[] postParas in webPostData.PostParaList)
                    {
                        listPostParas.Items.Add(new ListViewItem(postParas));
                    }
                }

                txtPostOtherParas.Text = webPostData.PostOtherParas;
                txtPostUsers.Text = webPostData.PostUsers;

                chkIsFtp.Checked = webPostData.IsFtp;
                chkIsFtpPasv.Checked = webPostData.IsFtpPasv;
                txtFtpServer.Text = webPostData.FtpServer;
                txtFtpPort.Text = webPostData.FtpPort.ToString();
                txtFtpUser.Text = webPostData.FtpUser;
                txtFtpPass.Text = webPostData.FtpPass;
                txtFtpDir.Text = webPostData.FtpDir;
                chkIsDelDownFile.Checked = webPostData.IsDelDownFile;

                cboTranslLang.SelectedValue = webPostData.TransLang;
                txtTransParas.Text = webPostData.TransParas;

                chkIsLogin.Checked = webPostData.IsLogin;
                txtLoginUrl.Text = webPostData.LoginUrl;
                txtPostCookies.Text = webPostData.PostCookies;
                txtPostOkCode.Text = webPostData.PostOkCode;
            }
        }

        private void btnParaUp_Click(object sender, EventArgs e)
        {

        }

        private void btnParaDown_Click(object sender, EventArgs e)
        {

        }

        private void btnParaAdd_Click(object sender, EventArgs e)
        {

        }

        private void btnParaUpdate_Click(object sender, EventArgs e)
        {

        }

        private void btnParaDelete_Click(object sender, EventArgs e)
        {
            deleteSelectedParaList();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void btnOk_Click(object sender, EventArgs e)
        {

        }

        private void btnGetCookies_Click(object sender, EventArgs e)
        {

        }

        private void treeViewWebPost_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                // load postdata
                if(treeViewWebPost.SelectedNode != null)
                {
                    string ClassID = treeViewWebPost.SelectedNode.Name.ToString();
                    if(ClassID != "" && ClassID.Substring(0, 1) != "C")
                    {
                        loadPostData(int.Parse(ClassID));
                    }
                }
            }
            catch(Exception ex)
            {
                DialogResult result = Utils.ShowMessageBox(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {

        }

        private void deleteSelectedParaList()
        {
        }

        private void chkIsReply_CheckedChanged(object sender, EventArgs e)
        {
            txtUrlReply.Enabled = chkIsReply.Checked;
        }

        private void chkIsLoginPost_CheckedChanged(object sender, EventArgs e)
        {
            bool isLogingPost = chkIsLogin.Checked;


            txtLoginUrl.Enabled = isLogingPost;
            btnGetCookies.Enabled = isLogingPost;
            txtPostCookies.Enabled = isLogingPost;
        }

        private void btnParaAdd_Click_1(object sender, EventArgs e)
        {
            
        }


    }
}
